/* eslint-disable prettier/prettier */
<template>
  <div class="body">
    <el-card class="table-container" v-if="!showDetail && !showNew">
      <div style="display: flex; justify-content: center">
        <span class="text">路线管理</span>
      </div>
      <el-divider></el-divider>
      <div style="width: 100%; display: flex; justify-content: start">
        <el-button type="primary" @click="showNew = true">新建路线</el-button>
      </div>
      <br />
      <el-table :data="MapList" border class="custom-table" v-loading="loading">
        <el-table-column type="selection" :selectable="selected" width="55" />
        <el-table-column label="路线创建时间">
          <template #default="scope">
            <span>{{ formatDate(scope.row.mapDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="mapName" label="路线名称"></el-table-column>
        <el-table-column label="操作" width="200">
          <!--#default="{ row }"-->
          <template #default="{ row }">
            <el-button @click="OpenDetail(row)" size="small">查看</el-button>
            <el-button type="primary" @click="EditMap(row)" size="small">编辑</el-button>
            <el-button type="danger" @click="DeleteMap(row)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <div style="width: 100%; display: flex; justify-content: end">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          :pager-count="7"
          layout="jumper, prev, pager, next, total"
          :total="totalItems"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!--路线详情页-->
    <DetailMap
      v-else-if="showDetail"
      :map-details="detailMarkerList"
      :map-info="currentMap"
      @turn-back="closeDetail"
      @refresh-data="getMapDetails(currentMap.mapId)"
    ></DetailMap>
    <!--新建路线页-->
    <NewMap v-else @turn-back="closeNew"></NewMap>
    <!--任务修改抽屉-->
    <el-drawer v-model="showEditDrawer" title="修改路线名称" :append-to-body="true" size="30%">
      <!-- 修改表单 -->
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="路线名称">
          <el-input v-model="editForm.mapName" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="ConfirmEdit">修改</el-button>
          <el-button type="info" @click="showEditDrawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script setup lang="js">
import DetailMap from "@/components/MyMap/detailMap/index.vue";
import NewMap from "@/components/MyMap/newMap/index.vue";
import { onMounted, reactive, ref } from "vue";
//import { Delete, Search, Edit } from "@element-plus/icons-vue";
import request from "@/api/request.js";
import { ElMessage, ElMessageBox } from "element-plus";

//value
const currentPage = ref(1);
const pageSize = ref(10);
const totalItems = ref(0);
const showDetail = ref(false);
const showNew = ref(false);
const showEditDrawer = ref(false);
const loading = ref(true);
const editForm = reactive({
  mapId: 0,
  mapName: ""
});
const MapList = ref([]);
const detailMarkerList = ref([]);
const currentMap = ref({});
const formatDate = date => {
  const d = new Date(date);

  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, "0");
  const day = String(d.getDate()).padStart(2, "0");

  const hours = String(d.getHours()).padStart(2, "0");
  const minutes = String(d.getMinutes()).padStart(2, "0");
  const seconds = String(d.getSeconds()).padStart(2, "0");

  return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
};
//getData
const getMapData = (page, size) => {
  loading.value = true;
  request
    .get("/api-self/inspection/map", { params: { pageNum: page, pageSize: size } })
    .then(res => {
      const data = res.data;
      if (data.code === 0) {
        MapList.value = data.data.maps;
        totalItems.value = data.data.total;
        console.log(totalItems.value);
      } else {
        ElMessage.error("获取路线失败：" + data.message);
      }
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
};
const getMapDetails = async mapId => {
  await request.get("/api-self/inspection/map/" + mapId).then(res => {
    const data = res.data;
    if (data.code === 0) {
      detailMarkerList.value = data.data;
    } else {
      ElMessage.error("获取路线点位信息失败：" + data.message);
    }
  });
};
//mounted
onMounted(() => {
  getMapData(1, pageSize.value);
});
//method
const OpenDetail = async row => {
  console.log("Open:", row);
  detailMarkerList.value = [];
  currentMap.value = row;
  await getMapDetails(currentMap.value.mapId);
  showDetail.value = true;
};
const EditMap = row => {
  console.log("Edit:", row);
  showEditDrawer.value = true;
  editForm.mapId = row.mapId;
  editForm.mapName = row.mapName;
};
const ConfirmEdit = () => {
  request.put("/api-self/inspection/map", editForm).then(res => {
    const data = res.data;
    if (data.code === 0) {
      ElMessage.success("修改成功");
      showEditDrawer.value = false;
      getMapData(currentPage.value, pageSize.value);
    } else {
      ElMessage.error("修改失败:" + data.message);
    }
  });
};
const DeleteMap = row => {
  console.log("Delete:", row);
  ElMessageBox.confirm("将会删除这个路线，确定吗？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    request.delete("/api-self/inspection/map/" + row.mapId).then(res => {
      const data = res.data;
      if (data.code === 0) {
        ElMessage.success("删除成功");
        getMapData(currentPage.value, pageSize.value);
      } else {
        ElMessage.error("删除失败:" + data.message);
      }
    });
  });
};
const closeDetail = () => {
  showDetail.value = false;
};
const closeNew = () => {
  showNew.value = false;
  currentMap.value = 1;
  getMapData(currentMap.value, pageSize.value);
};
const handleCurrentChange = val => {
  currentPage.value = val;
  getMapData(currentPage.value, pageSize.value);
};
const selected = () => {
  return true;
};
</script>

<style scoped>
.body {
  width: 100%;
  height: 100%;
}
.text {
  margin: 20px 0 0;
  font-size: 23px;
  font-weight: bold;
  color: var(--el-text-color-regular);
}
.custom-table :deep(.el-table__header-wrapper thead tr th) {
  color: #000000;
  text-align: center;
  background-color: #fafafa;
}
.custom-table :deep(.el-table__body-wrapper tbody tr td) {
  text-align: center; /* 单元格内容居中 */
}
.el-button {
  border-radius: 6px;
}
</style>
